package com.lc.advertising.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.advertising.R;
import com.lc.advertising.app.AppActivity;
import com.lc.advertising.http.glide.GlideApp;
import com.lc.advertising.http.request.ShareListApi;
import com.lc.advertising.http.response.ShareListBeans;
import com.lc.advertising.other.IntentKey;
import com.lc.advertising.util.SPUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;

public class SpreadDataActivity extends AppActivity {
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private int mPage=1;
    private int total;
    private BaseQuickAdapter<ShareListBeans.ShareListBean, BaseViewHolder> adapter;
   private List<ShareListBeans.ShareListBean> dataList=new ArrayList<>();
    @Override
    protected int getLayoutId() {
        return R.layout.activity_spreaddata;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        adapter = new BaseQuickAdapter<ShareListBeans.ShareListBean, BaseViewHolder>(R.layout.item_spread) {
            @Override
            protected void convert(@NotNull BaseViewHolder holder, ShareListBeans.ShareListBean dataBean) {
                GlideApp.with(SpreadDataActivity.this)
                        .load(dataBean.getHeadimg())
                        .circleCrop()
                        .into((ImageView) holder.getView(R.id.img_head));
                holder.setText(R.id.tv_user,dataBean.getNickname());
                holder.setText(R.id.tv_date,dataBean.getReg_time());


            }
        };
        View emptyview = View.inflate(this, R.layout.empty_emptylayout, null);
        adapter.setEmptyView(emptyview);
        recyclerview.setAdapter(adapter);
      share_list(mPage);
        refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(final @NonNull RefreshLayout refreshLayout) {
                refreshLayout.getLayout().postDelayed(() -> {
                    mPage++;
                    if (mPage <total) {
                        share_listmore(mPage);

                        refreshLayout.finishLoadMore();
                    } else {
                        toast("数据全部加载完毕");
                        refreshLayout.finishLoadMoreWithNoMoreData();//将不会再次触发加载更多事件
                    }
                }, 1000);
            }

            @Override
            public void onRefresh(final @NonNull RefreshLayout refreshLayout) {
                refreshLayout.getLayout().postDelayed(() -> {
                    mPage = 1;
                    share_list(mPage);
                    refreshLayout.finishRefresh();
                }, 1000);

            }
        });
    }

    private void share_listmore(int mPage) {
        EasyHttp.post(this)
                .api(new ShareListApi().setUid(SPUtil.get(this, IntentKey.UID,"")).setPage(mPage))
                .request(new HttpCallback<ShareListBeans>(this) {



                    @Override
                    public void onSucceed(ShareListBeans data) {

                        if (data.getCode()==200){
                            total=data.getTotal_count();

                            dataList.addAll(data.getShare_list());
                            adapter.setNewData(data.getShare_list());

                        }


                    }

                });
    }

    private void share_list(int page) {
        EasyHttp.post(this)
                .api(new ShareListApi().setUid(SPUtil.get(this, IntentKey.UID,"")).setPage(page))
                .request(new HttpCallback<ShareListBeans>(this) {



                    @Override
                    public void onSucceed(ShareListBeans data) {

                        if (data.getCode()==200){
                            total=data.getTotal_count();
                            dataList.clear();
                            dataList.addAll(data.getShare_list());
                            adapter.setNewData(data.getShare_list());

                        }


                    }

                });
    }
}
